 
var map, marker, gmarkers = [];

  function initialize() {
    var myLatlng = new google.maps.LatLng(51.1657, 10.4515);
    var myOptions = {
      zoom: 4,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
 
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
 
$('#markerTypes input[type="checkbox"]').bind('click', function () {
 
var markersType = $(this).val();
 
if($(this).attr("checked")) {
 
if(!gmarkers[markersType]) {
 
gmarkers[markersType] = [];
 
$.getJSON("/ajax/index", {markersType:markersType}, function(data){
 
setMarkers(data, markersType);				
 
});
 
}
else {
show(markersType);
}
 
}
else {
hide(markersType);
}
});	
 
     var html = "<table>" +
                         "<tr><td>Наименование:</td> <td><input type='text' id='name'/> </td> </tr>" +
                         "<tr><td>Адрес:</td> <td><input type='text' id='address'/></td> </tr>" +
                         "<tr><td>Тип:</td> <td><select id='type'>" +
                         "<option value='bar' SELECTED>Бар</option>" +
                         "<option value='restaurant'>Ресторан</option>" +
                         "<option value='cafe'>Кафе</option>" +
                         "</select> </td></tr>" +
                         "<tr><td></td><td><input type='button' value='Сохранить' onclick='saveData()'/></td></tr></form>";
    infowindow = new google.maps.InfoWindow({
     content: html
    });

  }

    function downloadUrl(url, callback) {
      var request = window.ActiveXObject ?
          new ActiveXObject('Microsoft.XMLHTTP') :
          new XMLHttpRequest;
 
      request.onreadystatechange = function() {
        if (request.readyState == 4) {
          request.onreadystatechange = doNothing;
          callback(request.responseText, request.status);
        }
      };
 
      request.open('GET', url, true);
      request.send(null);
    }
 
    function doNothing() {}	
 
	function setMarkers(data, category) {
 
	var infoWindow = new google.maps.InfoWindow;	 
	//var baseIcon = customIcons[category];	
 
	var marker_point = new Array();
        var html = new Array();
 
	for(var i = 0; i < data.markers.length; i++) {	
    	marker_point[i] = new google.maps.LatLng(data.markers[i].lat, data.markers[i].lon);	
	html[i] = '<strong>'+data.markers[i].mname+'</strong><br />'+data.markers[i].address;
        icon = data.markers[i].mimg;
	createMarker(marker_point[i], html[i], icon, infoWindow, category);
	}
 
}

function createMarker(point, html, icon, infoWindow, category) {
	 var marker = new google.maps.Marker({
            map: map,
            position: point,
             icon: '../../uploads/category/'+icon //+icon.icon
           // shadow: icon.shadow           
          });
 
  marker.mycategory = category; 
 
  google.maps.event.addListener(marker, 'click', function() {
        infoWindow.setContent(html);
        infoWindow.open(map, marker);
      });
  gmarkers.push(marker);
  return marker;
}
 
 
function show(category) {
        for (var i=0; i<gmarkers.length; i++) {
          if (gmarkers[i].mycategory == category) {
            gmarkers[i].setVisible(true);
          }
        }
        // == check the checkbox ==
        document.getElementById(category+"box").checked = true;
      }
 
      // == hides all markers of a particular category, and ensures the checkbox is cleared ==
      function hide(category) {
        for (var i=0; i<gmarkers.length; i++) {
          if (gmarkers[i].mycategory == category) {
            gmarkers[i].setVisible(false);
          }
        }
        // == clear the checkbox ==
        document.getElementById(category+"box").checked = false;
        // == close the info window, in case its open on a marker that we just hid
        infowindow.close();
      }
 
     
 